﻿using CommandService.Implementation.Base;
using DBService;
using DBService.Handler;
using Entity;
using Entity.SysEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandService.Implementation
{
    public class CommandImplementation : ICommands
    {
       Common common;
       IDbServices ser;
       public CommandImplementation()
       {
           common = new Common();
           ser = new DbHandler();
       }
       /// <summary>
       /// 根据实体对象查询数据列表
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="obj"></param>
       /// <returns></returns>
       public virtual List<T> GetDataByObject<T>(object obj)
       {
           return common.TableTolist<T>(ser.Query(obj));
       }
        /// <summary>
        /// 根据查询模型获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
       public virtual List<T> QueryData<T>(QueryModel model)
       {
           return common.TableTolist<T>(ser.QueryList(model));
       }
        /// <summary>
        /// 添加方法(单表)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
       public ReturnResult Add(object obj)
       {
           List<OperateModel> list = new List<OperateModel>();
           OperateModel oper = new OperateModel();
           oper.Data = obj;
           oper.Types = OperateModel.OperateType.Add;
           list.Add(oper);
           return ser.TransUpdate(list);
       }
        /// <summary>
        /// 修改方法(单表)
        /// </summary>
        /// <param name="oper"></param>
        /// <returns></returns>
       public ReturnResult Edit(OperateModel oper)
       {
           List<OperateModel> list = new List<OperateModel>();
           oper.Types = OperateModel.OperateType.Edit;
           list.Add(oper);
           return ser.TransUpdate(list);
       }
        /// <summary>
       /// 删除方法(单表)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
       public ReturnResult Del(object obj)
       {
           List<OperateModel> list = new List<OperateModel>();
           OperateModel oper = new OperateModel();
           oper.Data = obj;
           oper.Types = OperateModel.OperateType.Delete;
           list.Add(oper);
           return ser.TransUpdate(list);
       }
       #region 多表事物更新
       /// <summary>
        /// 多表事物更新
        /// </summary>
        /// <param name="list">操作集合</param>
        /// <returns></returns>
       public ReturnResult MoreUpdate(List<OperateModel> list)
       {
           return ser.TransUpdate(list);
       }
       #endregion

       public List<TableCloumns> TableStructure(string tableName)
       {
           return common.TableTolist<TableCloumns>(ser.TableStructure(tableName));
       }

       public ReturnResult UpdateSql(string sql)
       {
           return ser.UpdataBySql(sql);
       }

    }
}
